package c.x.jy.service.service.impl;

import c.x.jy.dao.dto.FanEntityDto;
import c.x.jy.dao.entity.MemberEntity;
import c.x.jy.dao.entity.TeacherEntity;
import c.x.jy.service.service.MemberService;
import c.x.jy.service.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import c.x.jy.common.utils.PageUtils;
import c.x.jy.common.utils.Query;

import c.x.jy.dao.dao.FanDao;
import c.x.jy.dao.entity.FanEntity;
import c.x.jy.service.service.FanService;
import org.apache.commons.lang3.StringUtils;


@Service("fanService")
public class FanServiceImpl extends ServiceImpl<FanDao, FanEntity> implements FanService {
    @Autowired
    TeacherService teacherService;
    @Autowired
    MemberService memberService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        	QueryWrapper<FanEntity> query= 	 new QueryWrapper<FanEntity>() ;
    	if(params.get("searchKey")!=null&&params.get("searchValue")!=null&&!StringUtils.isEmpty(params.get("searchKey").toString())&&!StringUtils.isEmpty(params.get("searchValue").toString()))
    	    query.eq(  params.get("searchKey").toString(), params.get("searchValue"));
 
        IPage<FanEntity> page = this.page(
                new Query<FanEntity>().getPage(params),
                 query
        );

        return new PageUtils(page);
    }

    @Override
    public FanEntity queryinfo(Long teacherId,Long fanId) {
        QueryWrapper<FanEntity> query= 	 new QueryWrapper<FanEntity>() ;

        if(teacherId!=null) 	query.lambda().eq(FanEntity::getTeacherId,teacherId);
        if(fanId!=null) 	query.lambda().eq(FanEntity::getFanId,fanId);
        List<FanEntity> list = this.baseMapper.selectList(query);

        return (list==null||list.isEmpty())?null:list.get(0);
    }

    @Override
    public void CancelGZ(Long teacherId, Long fanid) {
        QueryWrapper<FanEntity> query= 	 new QueryWrapper<FanEntity>() ;
        if(teacherId!=null) 	query.lambda().eq(FanEntity::getTeacherId,teacherId);
        if(fanid!=null) 	query.lambda().eq(FanEntity::getFanId,fanid);
        this.baseMapper.delete(query);
    }

    @Override
    public List<FanEntityDto> myRenmai() {
        TeacherEntity teacherEntity = teacherService.getByMemberId(memberService.getCurrUser().getId());
        Long teacherId=0l;
        if(teacherEntity!=null)
        teacherId = teacherEntity.getId();

//        QueryWrapper<FanEntity> query= 	 new QueryWrapper<FanEntity>() ;

//        if(teacherEntity!=null) 	query.lambda().eq(FanEntity::getTeacherId,teacherId);
//        List<FanEntity> list = this.baseMapper.selectList(query);
        List<FanEntityDto> list = this.baseMapper.myRenmai(teacherId);
        return list;
    }

    @Override
    public List<FanEntityDto> myGuanzhu() {
//        QueryWrapper<FanEntity> query= 	 new QueryWrapper<FanEntity>() ;
//
//        if(myid!=null) 	query.lambda().eq(FanEntity::getFanId,myid);
//        List<FanEntity> list = this.baseMapper.selectList(query);
        MemberEntity memberEntity = memberService.getCurrUser();
        Long memberId=0l;
        if(memberEntity!=null){
            memberId=memberEntity.getId();
        }
        List<FanEntityDto> list = this.baseMapper.myGuanzhu(memberId);
        return list;
    }

    @Override
    public PageUtils listforweb(Map<String, Object> params) {
        QueryWrapper<FanEntity> query= 	 new QueryWrapper<FanEntity>() ;
        if(params.get("searchKey")!=null&&params.get("searchValue")!=null&&!StringUtils.isEmpty(params.get("searchKey").toString())&&!StringUtils.isEmpty(params.get("searchValue").toString()))
            query.eq(  params.get("searchKey").toString(), params.get("searchValue"));
 
        IPage<FanEntityDto> page = this.getBaseMapper().listforweb(
                new Query<FanEntityDto>().getPage(params),
                query
        );

        return new PageUtils(page);
    }
}
